package com.navi.loaddata.core.util;

import com.navi.loaddata.core.mysqlEntity.EtlConfDModel;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.regex.Pattern;

/**
 * Created by Jay on 2018/3/17.
 */
public class ProcessUtil {

    /**
     * String to long
     * @param str
     * @return
     */
    public static Long getNum(String str) {
        Long num;
        try {
            num = Long.valueOf(str);
            return num;
        } catch (Exception e) {
            num = Long.valueOf(-1);
            return num;
        }
    }
    public static Double getDouble(BigDecimal d) {
        Double num;
        try {
            num = d.doubleValue();
            return num;
        } catch (Exception e) {
            num = Double.valueOf(-1);
            return num;
        }
    }
    public static Double getDouble(String str) {
        Double d;
        try {
            d = Double.valueOf(str);
            return d;
        } catch (Exception e) {
            return -1.0;
        }
    }



    /**
     * Integer to long
     * @param inte
     * @return
     */
    public static Long getNumI(Integer inte) {
        Long num;
        try {
            num = inte.longValue();
            return num;
        } catch (Exception e) {
            num = Long.valueOf(-1);
            return num;
        }
    }
    public static Integer getStr2Cnt(String cnt) {
        Integer num;
        try {
            num = Integer.valueOf(cnt);
            return num;
        } catch (Exception e) {
            return 0;
        }
    }



    public static BigDecimal getBigDecimal(String thickness) {
        double num;
        try {
            num = Double.valueOf(thickness);
            return BigDecimal.valueOf(num);
        } catch (Exception e) {
            return BigDecimal.valueOf(-1);
        }
    }
    public static BigDecimal getBigDecimal(BigDecimal thickness) {
        if (!StringUtils.isEmpty(thickness)){
            return thickness;
        }
        return BigDecimal.valueOf(-1);
    }

    public static BigDecimal getBigDecimal(Float thickness) {
        if (!StringUtils.isEmpty(thickness)){
            return BigDecimal.valueOf(thickness);
        }
        return BigDecimal.valueOf(-1);
    }


    /**
     * 空字符串 和 包含*的处理
     * @param str
     * @return
     */
    public static String processString(String str) {
        if (StringUtils.isEmpty(str) || str.contains("*")) {
            return " ";
        } else {
            return str;
        }
    }

    public static String processString2(String str) {
        if (StringUtils.isEmpty(str)) {
            return " ";
        } else {
            return str;
        }
    }

    public static boolean isNumeric(String str){
        Pattern pattern = Pattern.compile("[0-9]*");
        return pattern.matcher(str).matches();
    }

    /**
     * 厂别，站点,机台 空字符串 和 包含*的处理,GP
     * @param confShop,confOpe,confEqpt
     * @return
     */
    public static String getShopHql(String confShop) {
        if(StringUtils.isEmpty(confShop)){
            return "";
        }
        StringBuffer hql = new StringBuffer();
        hql.append(" and shopId like'"+StringUtils.replace(confShop,"*","%")+"' ");
        return hql.toString();
    }

    public static String getOpeHql(String confOpe) {
        if(StringUtils.isEmpty(confOpe)){
            return "";
        }
        StringBuffer hql = new StringBuffer();
            hql.append(" and opeId like'"+StringUtils.replace(confOpe,"*","%")+"' ");
        return hql.toString();
    }

    public static String getEqptHql(String confEqpt) {
        if(StringUtils.isEmpty(confEqpt)){
            return "";
        }
        StringBuffer hql = new StringBuffer();
        hql.append(" and eqptId like'"+StringUtils.replace(confEqpt,"*","%")+"' ");
        return hql.toString();
    }


    /**
     * 厂别，站点,机台 空字符串 和 包含*的处理,MES
     * @param confShop,confOpe,confEqpt
     * @return
     */
    public static String getShopMesHql(String confShop) {
        if(StringUtils.isEmpty(confShop)){
            return "";
        }
        StringBuffer hql = new StringBuffer();
        hql.append(" and SUBFACTORYNAME like'"+StringUtils.replace(confShop,"*","%")+"' ");
        return hql.toString();
    }

    public static String getOpeMesHql(String confOpe) {
        if(StringUtils.isEmpty(confOpe)) return "";
        StringBuffer hql = new StringBuffer();
        hql.append(" and OLDPROCESSOPERATIONNAME like'"+StringUtils.replace(confOpe,"*","%")+"' ");
        return hql.toString();
    }

    public static String getEqptMesHql(String confEqpt) {
        if(StringUtils.isEmpty(confEqpt)) return "";

        StringBuffer hql = new StringBuffer();
        hql.append(" and MACHINENAME like'"+StringUtils.replace(confEqpt,"*","%")+"' ");
        return hql.toString();
    }
    public static String getEqptMesHisHql(String confEqpt) {
        if(StringUtils.isEmpty(confEqpt))return "";
        StringBuffer hql = new StringBuffer();
        hql.append(" and unitname like'"+StringUtils.replace(confEqpt,"*","%")+"' ");
        return hql.toString();
    }
    public static String getUnitEqptIdHql(String confEqpt) {
        if(StringUtils.isEmpty(confEqpt))return "";
        StringBuffer hql = new StringBuffer();
        hql.append(" and unitname like'"+StringUtils.replace(confEqpt,"*","%")+"' ");
        return hql.toString();
    }








    /**
     *  空字符串 和 包含*的处理
     * @param confShop
     * @return
     */


    /**
     * 站点 判断第六码是为D（true）或者以DUM开头的站点
     * @param opeId
     * @return
     */
    public static boolean opeLimit(String opeId) {
        if ((opeId.length() > 5 && "D".equals(opeId.substring(5, 6)))) {
            return true;
        }
        if("DUM".equals(opeId.substring(0,3))){
            return true;
        }
        return false;
    }

    public static String getStringS(String ng_mark) {
        if (StringUtils.isEmpty(ng_mark)){
            return null;
        }
        return ng_mark;
    }

    public static Timestamp getTimestamp(Timestamp logon_timestamp) {
        if (StringUtils.isEmpty(logon_timestamp)){
            return null;
        }
        return logon_timestamp;
    }


    //转4码
    public static String getLength4(int defectNo) {
        String str=String.valueOf(defectNo);
        switch (str.length()){
            case 1:str= "000"+str;break;
            case 2:str= "00"+str;break;
            case 3:str= "0"+str;break;
        }
        return str;
    }

    public static String glassIdLimitMes(EtlConfDModel etlConfD) {
        String sql="";
        switch (etlConfD.getShop()){
            case "ARRAY":sql=" and length(productname)=10";
            case "CF": sql= " and length(productname)=10";
            case "OC": if("CELL1".equals(etlConfD.getCellType())){
                sql= " and length(productname)=10";
            }else if("CELL2".equals(etlConfD.getCellType())){
                sql= " and length(productname)=12";
            }
        }
        return sql;
    }
}
